import Vue from 'vue'
import CryptoJS from 'crypto-js'
import JsEncrypt from 'jsencrypt'
import myStore from '@/pages/store'
export default {
	//获取随机数
	getRandomKey(size) {
		let i,j;
		let seed = new Array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
			'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0',
			'1', '2', '3', '4', '5', '6', '7', '8', '9');
		let seedlength = seed.length;
		let keyNum = '';
		for(i = 0; i < size; i++) {
			j = Math.floor(Math.random() * seedlength);
			keyNum += seed[j];
		}
		return CryptoJS.enc.Utf8.parse(keyNum);
	},
	
	//aes加密
	encrypt(message, key, iv) {
	 	let encrypted = CryptoJS.AES.encrypt(message, key,{
	 		iv: CryptoJS.enc.Utf8.parse(iv),
	 		mode: CryptoJS.mode.CBC,
	 		padding: CryptoJS.pad.Pkcs7
	 	});
	 	return encrypted.toString();
	},
	 //aes解密
	 decrypt(message, key, iv) {
	 	let decrypt = CryptoJS.AES.decrypt(message, key, {
			 iv: CryptoJS.enc.Utf8.parse(iv),
			 mode: CryptoJS.mode.CBC,
			 padding: CryptoJS.pad.Pkcs7
	 	});
	 	return decrypt.toString(CryptoJS.enc.Utf8);
	 },
	 //rsa加密
	 getRsa(message){
		 let enc = new JSEncrypt();
		 if(myStore.baseUrl=="https://open.njcb.com.cn"){
			enc.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDJ+Ujosp/fiR3oGJTpeFrzCUx6v8u5OKDy+IVXWlhxLqebVNvZKnKazTNhMIRAIQJvmAO5jXxdFXLibVc9qIB5Zfv4pckk+eLuq/qyU4QONLuA3KdA+0skr1SHtVT3coep9xJLVyDg1o/5e7whHDKHbzAav1Kyp03pnwC5jmVXRQIDAQAB');
		 }else{	 
			enc.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQspyMVBCi29sTOiqZWZWitKdz+aLGu5FArOhY4DiYlAC1EqDI3c3F07M0oQ30RYLtKgxqB828g+knmFZK7nZ4ADy/f8ldMzx6v6W5YiVmzNssZQaC7keUOuEjWniPlMKMjWi6bSLfaNkinx5HiDPr8kqc9nDiR5eqw7kMI+iuawIDAQAB');
		}
		let encrypte = enc.encrypt(message);
		return encrypte;
	 }

}